package source;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.text.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.OracleTypes;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author BrunoCardoso
 */
public class DatabaseHandler {

    final String username = "bd01";
    final String password = "bd01";
    final String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    private Connection conn;
    private Statement stm;

    public DatabaseHandler() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        this.conn = DriverManager.getConnection(url, username, password);
        this.stm = conn.createStatement();
    }

    public String checkUser(String username,String password){
        try {
            CallableStatement cstmt = conn.prepareCall("{?=call checkPESSOA (?,?)}");
            cstmt.registerOutParameter(1,Types.VARCHAR);
            cstmt.setString(2, username);
            cstmt.setString(3, password);
            cstmt.execute();          
            String result=cstmt.getString(1);
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "false";
    }
    public String checkBanido(int id,int idSala){
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{?=call checkbanido (?,?)}");
            cstmt.registerOutParameter(1,Types.VARCHAR);
            cstmt.setInt(2, id);
            cstmt.setInt(3, idSala);
            cstmt.execute();          
            result=cstmt.getString(1);
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "false";
    }
    public String checkRestrito(int id,int idSala){
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{?=call CHECKPERMISSOES (?,?)}");
            cstmt.registerOutParameter(1,Types.VARCHAR);
            cstmt.setInt(2, id);
            cstmt.setInt(3, idSala);
            cstmt.execute();          
            result=cstmt.getString(1);
            System.out.println(result);
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "false";
    }
    public String checkUserToRegistry(String usernameR){
        ResultSet rs;
        
        String query="SELECT utilizador FROM pessoa WHERE upper(utilizador) LIKE upper('" +usernameR+ "')" ;
        
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                String user=rs.getString("utilizador");
                return user;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "null";
    }
    
    public boolean addUser(String nome, String username, String password, String data,String sexo, String email,String pais, String cidade, String interesses,String url ,boolean publico, boolean administrator){
        java.sql.Date date=null;
        DateFormat formatter = new SimpleDateFormat("yy/MM/dd");  
        try {  
            date = new java.sql.Date( ((java.util.Date)formatter.parse(data)).getTime() );
        } catch (ParseException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            CallableStatement cstmt = conn.prepareCall("{call ADDPESSOA (?,?,?,?,?,?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1, 0);
            cstmt.setString(2, nome);
            cstmt.setString(3, username);
            try {
                cstmt.setString(4, PasswordEncrypt.MD5(password));
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            cstmt.setDate(5, date);
            cstmt.setString(6, sexo);
            cstmt.setString(7, email);
            cstmt.setString(8, pais);
            cstmt.setString(9, cidade);
            cstmt.setString(10, interesses);
            cstmt.setString(11, url);
            cstmt.setBoolean(12, publico);
            cstmt.setBoolean(13, administrator);
            cstmt.executeUpdate();
            
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }       
        return true;
    }
        public boolean removeUser(String username,String password){
        try {
            CallableStatement cstmt = conn.prepareCall("{call DELETEPESSOA (?,?)}");
            cstmt.setString(1, username);
            cstmt.setString(2, password);
            cstmt.executeUpdate();
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
        public boolean removeUserCOMPLETO(String username,String password){
        try {
            CallableStatement cstmt = conn.prepareCall("{call DELETEPESSOACOMPLETA (?,?)}");
            cstmt.setString(1, username);
            cstmt.setString(2, password);
            cstmt.executeUpdate();
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        }
        
        public boolean removeMENSAGEMENVIADA(int id_mensagem){
        try {
            CallableStatement cstmt = conn.prepareCall("{call DELETEPMENSAGEMENVIADA (?)}");
            cstmt.setInt(1, id_mensagem);
            cstmt.executeUpdate();
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
        public boolean removeMENSAGEMENRECEBIDAS(int id_mensagem){
        try {
            CallableStatement cstmt = conn.prepareCall("{call DELETEPMENSAGEMENVIADA (?)}");
            cstmt.setInt(1, id_mensagem);
            cstmt.executeUpdate();
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    public boolean updateUser(int id,String nome, String username, String password, String data,String sexo, String email,String pais, String cidade, String interesses,String urlFoto,boolean publico, boolean administrator){
        java.sql.Date date=null;
        data=data.replaceAll("-", "/");
        DateFormat formatter = new SimpleDateFormat("yy/MM/dd");  
        try {  
            date = new java.sql.Date( ((java.util.Date)formatter.parse(data)).getTime() );
        } catch (ParseException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            CallableStatement cstmt = conn.prepareCall("{call UPDATEPESSOA (?,?,?,?,?,?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1,id);
            cstmt.setString(2, nome);
            cstmt.setString(3, username);
            cstmt.setString(4, password);
            cstmt.setDate(5, date);
            cstmt.setString(6, sexo);
            cstmt.setString(7, email);
            cstmt.setString(8, pais);
            cstmt.setString(9, cidade);
            cstmt.setString(10, interesses);
            cstmt.setString(11, urlFoto);
            cstmt.setBoolean(12, publico);
            cstmt.setBoolean(13, administrator);
            cstmt.executeUpdate();         
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        return false;
    }
    public String getUser(String usernameR){
        ResultSet rs;
        String result="";
        
        try {
            String query="SELECT * FROM pessoa WHERE utilizador LIKE '"+usernameR+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getInt("id")+"#";
                result+=rs.getString("nome")+"#";
                result+=rs.getString("utilizador")+"#";
                result+=rs.getString("pass")+"#";
                result+=rs.getDate("data_nascimento")+"#";
                result+=rs.getString("sexo")+"#";
                result+=rs.getString("email")+"#";
                result+=rs.getString("pais")+"#";
                result+=rs.getString("cidade")+"#";
                result+=rs.getString("interesses")+"#";
                result+=rs.getString("fotos")+"#";
                result+=rs.getInt("publico")+"#";
                result+=rs.getInt("administrador");
                return result;
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "null";
    }
        public String getChatroom(String chatRoomName){
        ResultSet rs;
        String result="";
        
        try {
            String query="SELECT * FROM chatroom WHERE nome_sala LIKE '"+chatRoomName+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getInt("id_sala")+"#";
                result+=rs.getString("nome_sala")+"#";
                result+=rs.getString("utilizadorcriador")+"#";
                result+=rs.getString("tipo_sala")+"#";
                result+=rs.getDate("rate_sala")+"#";
                result+=rs.getString("visivel")+"#";
                return result;
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "null";
    }
    public String getPassword(String usernameR){
        
        ResultSet rs;
        String result="";
        
        try {
            String query="SELECT pass FROM pessoa WHERE utilizador LIKE '"+usernameR+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result=rs.getString("pass");
                return result;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public int getId(String usernameR){
        
        ResultSet rs;
        int result;
        
        try {
            String query="SELECT id FROM pessoa WHERE utilizador LIKE '"+usernameR+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result=rs.getInt("id");
                return result;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return -1;
    }
    
    public String getImageURL(String usernameR){
        
        ResultSet rs;
        String result;
        
        try {
            String query="SELECT FOTOS FROM pessoa WHERE utilizador LIKE '"+usernameR+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result=rs.getString("FOTOS");
                return result;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "null";
    }
    
    public String searchUser(String nome, String usernameR, String sexo, String email, String pais, String cidade){
        ResultSet rs;
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{call PROCURAPESSOA (?,?,?,?,?,?,?)}");
            
            cstmt.setString(1, nome);
            cstmt.setString(2, usernameR);
            cstmt.setString(3, sexo);
            cstmt.setString(4, email);
            cstmt.setString(5, pais);
            cstmt.setString(6, cidade);
            cstmt.registerOutParameter(7,OracleTypes.CURSOR);
            cstmt.execute();
            
            rs=(ResultSet)cstmt.getObject(7);
            while(rs.next()){
                result+=rs.getString("utilizador")+"#";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }
    
    public String addMENSAGEMPRIVADA(String usernameR, String usernameD, String data,String texto, String ficheiro){
        ResultSet rs;
        
        try {
            CallableStatement cstmt = conn.prepareCall("{?= call ADDMENSAGEM_PRIVADA (?,?,?,?,?,?,?)}");
            
            cstmt.registerOutParameter(1,Types.VARCHAR);
            cstmt.setInt(2, 0);
            cstmt.setString(3, usernameR);
            cstmt.setString(4, usernameD);
            cstmt.setString(5,"");
            cstmt.setString(6, texto);
            cstmt.setString(7, null);
            cstmt.setInt(8, 0);
            cstmt.execute();
            String resultado=cstmt.getString(1);
            cstmt.close();
            return resultado;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "false";
    }
    
     public String addMENSAGEMPRIVADADELAY(String usernameR, String usernameD, String data,String texto, String ficheiro){
        ResultSet rs;
        java.sql.Date date=null;
        DateFormat formatter = new SimpleDateFormat("dd/MM/yy hh:mm");  
        try {  
            date = new java.sql.Date( ((java.util.Date)formatter.parse(data)).getTime() );
        } catch (ParseException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            CallableStatement cstmt = conn.prepareCall("{?= call ADDMENSAGEM_PRIVADA_delay (?,?,?,?,?,?,?)}");
            
            cstmt.registerOutParameter(1,Types.VARCHAR);
            cstmt.setInt(2, 0);
            cstmt.setString(3, usernameR);
            cstmt.setString(4, usernameD);
            cstmt.setDate(5,date);
            cstmt.setString(6, texto);
            cstmt.setString(7, null);
            cstmt.setInt(8, 0);
            cstmt.execute();
            String resultado=cstmt.getString(1);
            cstmt.close();
            return resultado;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "false";
    }
     
    public String MENSAGENSENVIADAS( String usernameR){
        ResultSet rs;
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{?=call MENSAGENSENVIADAS (?)}");
            
            
            cstmt.registerOutParameter(1,OracleTypes.CURSOR);
            cstmt.setString(2, usernameR);
            cstmt.execute();
            
            rs=(ResultSet)cstmt.getObject(1);
            while(rs.next()){
                result+=rs.getInt("id_mensagensprivadas")+"#";
                result+="mensagem: ";
                result+=rs.getString("texto")+"#destinatário: ";
                result+=rs.getString("utilizadordestinatario")+"/\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }
    
    public String CHECKMENSAGEMHISTORIAL( String usernameR){
        ResultSet rs;
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{?=call CHECKMENSAGEMHISTORIAL (?)}");
            
            
            cstmt.registerOutParameter(1,OracleTypes.CURSOR);
            cstmt.setString(2, usernameR);
            cstmt.execute();
            
            rs=(ResultSet)cstmt.getObject(1);
            while(rs.next()){
                result+=rs.getInt("id_mensagensprivadas")+"#";
                result+="mensagem: ";
                result+=rs.getString("texto")+"#remetente: ";
                result+=rs.getString("utilizadorremetente")+"/\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }
    
    public String CHECKMENSAGEMPRIVADANOVA( String usernameR){
        ResultSet rs;
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{?=call CHECKMENSAGEMPRIVADANOVA (?)}");
            
            
            cstmt.registerOutParameter(1,OracleTypes.CURSOR);
            cstmt.setString(2, usernameR);
            cstmt.execute();
            
            rs=(ResultSet)cstmt.getObject(1);
            while(rs.next()){
                result+="mensagem: ";
                result+=rs.getString("texto")+"##remetente: ";
                result+=rs.getString("utilizadorremetente")+"/\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }
    
    public boolean UPDATEMENSAGENSPRIVADAS(String utilizadordestinatario){
                
        try {
            CallableStatement cstmt = conn.prepareCall("{call UPDATEMENSAGENSPRIVADAS (?)}");
            cstmt.setString(1, utilizadordestinatario);
            
            cstmt.executeUpdate();         
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
       
    }
    public String addChatroom(String chatRoomName,String usernameR,String tema ){
        
        try {
            CallableStatement cstmt = conn.prepareCall("{call CRIACHATROOM (?,?,?)}");
            cstmt.setString(1, chatRoomName);
            cstmt.setString(2, usernameR);
            cstmt.setString(3, tema);
            cstmt.execute();
            cstmt.close();
            return "true";
        } catch (SQLException ex) {          
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return "false";
        }
    }
    public String searchMyChatroom(String usernameR){
        ResultSet rs;
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{call PROCURACHATROOMcriador (?,?)}");
            
            cstmt.setString(1, usernameR);
            cstmt.registerOutParameter(2,OracleTypes.CURSOR);
            cstmt.execute();         
            rs=(ResultSet)cstmt.getObject(2);
            while(rs.next()){
                result+=rs.getString("nome_sala")+"#";
                result+=rs.getString("tipo_sala")+"#";
                result+=rs.getInt("visivel")+"/";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }
    public String searchChatroom(String nomeSala, String temaSala){
        ResultSet rs;
        String result="";
        try {
            CallableStatement cstmt = conn.prepareCall("{call PROCURACHATROOM (?,?,?)}");
            
            cstmt.setString(1, nomeSala);
            cstmt.setString(2, temaSala);
            cstmt.registerOutParameter(3,OracleTypes.CURSOR);
            cstmt.execute();         
            rs=(ResultSet)cstmt.getObject(3);
            while(rs.next()){
                result+=rs.getString("nome_sala")+"#";
                result+=rs.getString("tipo_sala")+"\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "";
    }
    public boolean updateChatroom(int id, String nome,String tema){
        
        try {
            CallableStatement cstmt = conn.prepareCall("{call UPDATECHATROOM (?,?,?)}");
            cstmt.setInt(1,id);
            cstmt.setString(2, nome);
            cstmt.setString(3, tema);
            cstmt.executeUpdate();         
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    public boolean newMessageChatroom(String nomeRemetente,int idSala,String mensagem,String ficheiro){
        
        try {
            CallableStatement cstmt = conn.prepareCall("{call ADDMENSAGENS (?,?,?,?,?)}");
            cstmt.setInt(1,0);
            cstmt.setString(2, nomeRemetente);
            cstmt.setInt(3, idSala);
            cstmt.setString(4, mensagem);
            cstmt.setString(5, ficheiro);
            cstmt.executeUpdate();         
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    public boolean setInvisibleChatroom(int idSala){
        
        try {
            String query="UPDATE chatroom SET visivel=0 where id_sala LIKE '"+idSala+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            cstmt.executeQuery();
            cstmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public String getMensagensSala(int idSala){
        ResultSet rs=null;
        String result="";
        try {
            String query="SELECT nome_remetente,textomensagem FROM mensagens WHERE id_sala LIKE '"+idSala+"' ORDER BY id_mensagens";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getString("nome_remetente")+": ";
                result+=rs.getString("textomensagem")+"\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "null";
    }
    public String getBanidosSala(int idSala){
        ResultSet rs=null;
        String result="";
        try {
            String query="SELECT id FROM banidos WHERE id_sala LIKE '"+idSala+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getInt("id")+"\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "null";
    }
    public String getRestritos(int idSala){
        ResultSet rs=null;
        String result="";
        try {
            String query="SELECT id FROM premissoes WHERE id_sala LIKE '"+idSala+"'";
            PreparedStatement cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getInt("id")+"\n";
            }
            cstmt.close();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "null";
    }
    public String getUsername(int idR){
        ResultSet rs;
        
        String query="SELECT utilizador FROM pessoa WHERE id LIKE upper('" +idR+ "')" ;
        
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                String user=rs.getString("utilizador");
                return user;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return "null";
    }
    public boolean addBanido(int idR, int idSala){
        
        
        String query="INSERT INTO banidos VALUES('" +idSala+ "','"+idR+"')";
        
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            cstmt.executeQuery();
            return true;
        } catch (SQLException ex) {
            
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    public boolean addRestrito(int idR, int idSala){
        
        
        String query="INSERT INTO premissoes VALUES('" +idR+ "','"+idSala+"')";
        
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            cstmt.executeQuery();
            return true;
        } catch (SQLException ex) {
            
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    public String getMessageChatrrom(int idSala){
        
        String result="";
        String query="SELECT nome_remetente,textomensagem FROM mensagens WHERE id_sala like ('" +idSala+"')";
        ResultSet rs;
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getString("nome_remetente")+": ";
                result+=rs.getString("textomensagem")+"\n";
                
            }
            return result;
        } catch (SQLException ex) {
            
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return "null";
        }
    }
    public String ListAllChatrrom(){
        
        String result="";
        String query="SELECT nome_sala,tipo_sala FROM chatroom WHERE visivel=1";
        ResultSet rs;
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getString("nome_sala")+"# ";
                result+=rs.getString("tipo_sala")+"\n";
                
            }
            return result;
        } catch (SQLException ex) {
            
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return "null";
        }
    }
    public String getNameChatroom(int id_salaR){
        
        String result="";
        String query="SELECT nome_sala FROM chatroom WHERE id_sala like '"+id_salaR+"'";
        ResultSet rs;
        PreparedStatement cstmt;
        try {
            cstmt = conn.prepareStatement(query);
            rs=cstmt.executeQuery();
            while(rs.next()){
                result+=rs.getString("nome_sala");             
            }
            return result;
        } catch (SQLException ex) {
            
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
            return "null";
        }
    }
}
